package com.facebank.usersupport.util;

import com.facebank.usersupport.service.impl.JsonpServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CollaborativeUtil {
    private static final Logger logger = LoggerFactory.getLogger(JsonpServiceImpl.class);

    public static Map outNearbyUserList(Long associatorId,long[] associatorIds,Map score) {
        Map scores = new HashMap();
        for (int i=0;i<associatorIds.length;i++) {
            if (associatorIds[i]==associatorId) {
                continue;
            }
            double goal = getOSScore(associatorId, associatorIds[i],score);
            scores.put(associatorIds[i], goal);
        }
        return scores;
        //System.out.println(scores.toString());
    }

    private static Double getOSScore(Long associatorId1, Long associatorId2,Map score) {
        HashMap user1Score = (HashMap) score.get(associatorId1);
        HashMap user2Score = (HashMap) score.get(associatorId2);
        double totalscore = 0.0;
        Iterator it = user1Score.keySet().iterator();
        while (it.hasNext()) {
            long activityId = (long) it.next();
            int a1 = Integer.parseInt(user1Score.get(activityId).toString()) ;
            int a2 = Integer.parseInt(user1Score.get(activityId).toString());
            int b1 = Integer.parseInt(user2Score.get(activityId).toString());
            int b2 = Integer.parseInt(user2Score.get(activityId).toString());
            int a = a1 * a2 - b1 * b2;
            //System.out.println(Math.abs(a));
            totalscore += Math.sqrt(Math.abs(a));
        }

        return totalscore;
    }
}
